'use strict';

module.exports = function(sequelize, DataTypes) {
    const pointDetail = sequelize.define('PointDetail', {
        id: {
            type: DataTypes.BIGINT(20).UNSIGNED,
            allowNull: false,
            autoIncrement: true,
            primaryKey: true,
            readonly: true
        },
        rule_id: {
            type: DataTypes.BIGINT(20).UNSIGNED,
            allowNull: false,
            comment: '积分规则id'
        },
        item_id: {
            type: DataTypes.BIGINT(20).UNSIGNED,
            allowNull: false,
            comment: '设备id'
        },
        user_id: {
            type: DataTypes.BIGINT(20).UNSIGNED,
            allowNull: false,
            comment: '用户id'
        },
        alteration: {
            type: DataTypes.FLOAT,
            comment: '积分变化'
        },
        log_time: {
            type: DataTypes.STRING(191),
            comment: '快照的createdAt'
        },
        relation_log_time: {
            type: DataTypes.STRING(191),
            comment: '关联的快照的createdAt'
        },
        relation_user_id: {
            type: DataTypes.BIGINT(20).UNSIGNED,
            comment: '被关联的用户id'
        },
        relation_alteration: {
            type: DataTypes.FLOAT,
            comment: '被关联的用户积分变化'
        },
        created_at: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW,
            readonly: true
        }
    }, {
        tableName: 'sophic_point_details',
        underscored: true,
        indexes: [
        ],

        classMethods: {

            //operationLog: true,

            associate: function(models) {
                pointDetail.belongsTo(models.PointRule, {
                    foreignKey: 'rule_id',
                    constraints: false,
                    as: 'Rule'
                })
            }
        }
    });

    return pointDetail;
};